package morning.cat.study.jdk.serializable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Calendar;

import org.junit.Test;

import morning.cat.study.Archive;

public class DemoTest {
	@Test
	public void writeObject1() throws IOException {
		User user = new User();
		user.setId(1001);
		user.setName("晨猫");
		user.setAddDate(Calendar.getInstance().getTime());
		user.setNo("Xs01");
		user.setArchive(new Archive("Mc001", "Hangzhou", "18366557620"));
		System.out.println(user);

		OutputStream outputStream = new FileOutputStream(new File("User.out"));
		ObjectOutputStream oos = new ObjectOutputStream(outputStream);
		oos.writeObject(user);
		oos.close();
	}

	@Test
	public void readObject1() throws IOException, ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("User.out"));
		User user = (User) ois.readObject();
		System.out.println(user);
		ois.close();
	}
	
	/**********************************/
	
	@Test
    public void writeObject2() throws IOException {
        User2 user = new User2();
        user.setId(1002);
        user.setName("龙猫");
        user.setAddDate(Calendar.getInstance().getTime());

        OutputStream outputStream = new FileOutputStream(new File("User2.out"));
        ObjectOutputStream oos = new ObjectOutputStream(outputStream);
        oos.writeObject(user);
        oos.close();
    }

    @Test
    public void readObject2() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("User2.out"));
        User2 user = (User2) ois.readObject();
        System.out.println(user);
        ois.close();
    }
}
